package com.qyer.lxp.common.framework.util;

import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.apache.log4j.Logger;

import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by jiangping on 16/5/6.
 */
public class QyerConfig {

    private static Logger logger = Logger.getLogger(QyerConfig.class);
    private static final String configureFile = "/configure.properties";
    private static final ReentrantLock lock = new ReentrantLock();

    public static PropertiesConfiguration getInstance()
    {
        PropertiesConfiguration instance = null;
        lock.lock(); // block until condition holds
        try
        {
            instance = new PropertiesConfiguration();
            instance.setEncoding("utf-8");
            instance.setURL(QyerConfig.class.getResource(configureFile));
            // DEFAULT_REFRESH_DELAY = 5000
            instance.setReloadingStrategy(new FileChangedReloadingStrategy());

            try
            {
                instance.load();
            } catch (Exception ee)
            {
                logger.error(ee.getClass().getName() + " _" + ee.getMessage());
            }
        } finally
        {
            lock.unlock();
        }

        return instance;
    }

    public static String getPath(String key)
    {
        return getInstance().getString(key, "."); // default current directory
    }
}
